package com.study.service; 

import java.util.List;
import javax.annotation.Resource;
import com.study.dao.ComplaintsDao;
import com.study.entity.Complaints;
import com.study.util.Tools;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;  
/**  
 *  
 */  
@Transactional
@Service
public class ComplaintsService  
{  
    /********** 注入ComplaintsDao ***********/  
    @Resource(name="complaintsDao")
	private ComplaintsDao complaintsDao;
	

	//返回的json数据的key
	private final static String RE_CODE_STR = "code";
	private final static String RE_DATA_STR = "data";
	private final static String RE_PAGE_STR = "pages";//数据共有多少页
	private final static String RE_COUNT_STR = "count";//数据的总数
	
	//返回的json数据的value
	//返回 0 表示操作成功
	private final static int RE_CODE_0 = 0;
	//返回 1表示操作失败
	private final static int RE_CODE_1 = 1;
	//返回 2表示没有数据
	private final static int RE_CODE_2 = 2;
    
    //增加一个Complaints
    public String saveComplaints(Complaints complaints){
		JSONObject resultObj = new JSONObject();
		complaintsDao.saveComplaints(complaints);
		
		resultObj.put(RE_CODE_STR, RE_CODE_0);
		return resultObj.toString();
		
	}
	
	
	//根据id查询Complaints
    public String getComplaintsById(String id){
    	JSONObject resultObj = new JSONObject();
        
        Complaints result = complaintsDao.getComplaintsById(id);
        if(result==null){
            resultObj.put(RE_CODE_STR, RE_CODE_2);
        }else{
            resultObj.put(RE_CODE_STR, RE_CODE_0);
            JSONObject jb = new JSONObject(result);
			jb.put("cpTime", Tools.date2StrShort(result.getCpTime()));
            resultObj.put(RE_DATA_STR, jb);
        }
        
        return resultObj.toString();
    }
	
	
	
	
	
	
	//分页条件查询Complaints
	public String listComplaints(Complaints complaints, String page, String number){
		JSONObject resultObj = new JSONObject();
		String from = null;
		if (Tools.notEmpty(page) && Tools.notEmpty(number)) {
			from = String.valueOf((Integer.parseInt(page)-1)*Integer.parseInt(number));
		}
		
	    List<Complaints> listPage = complaintsDao.listComplaints(complaints,from, number);
	    if (listPage.isEmpty()) {
			resultObj.put(RE_CODE_STR, RE_CODE_2);
		} else {
			JSONArray array = new JSONArray();
			for(int i=0,len=listPage.size();i<len;i++){
				Complaints item = listPage.get(i);
				array.put(i, new JSONObject(item));
				JSONObject jb = (JSONObject) array.get(i);
				jb.put("cpTime", Tools.date2StrShort(item.getCpTime()));
			}
		
	        resultObj.put(RE_CODE_STR, RE_CODE_0);
			resultObj.put(RE_DATA_STR, array);
			if (Tools.notEmpty(page) && Tools.notEmpty(number)) {
			    long count = complaintsDao.getComplaintsCount(complaints);
				resultObj.put(RE_PAGE_STR, Math.ceil(count/Double.parseDouble(number)));
				resultObj.put(RE_COUNT_STR, count);
			}
		}
		return resultObj.toString();
	}
	

		
	//根据id删除Complaints
	public String removeComplaintsById(String id){
		JSONObject resultObj = new JSONObject();
		boolean res =  complaintsDao.removeComplaintsById(id);
		if (res) {
			resultObj.put(RE_CODE_STR, RE_CODE_0);
		} else {
			resultObj.put(RE_CODE_STR, RE_CODE_1);
		}
		return resultObj.toString();
	}
	
	//更新Complaints
	public String updateComplaints(Complaints complaints){
		JSONObject resultObj = new JSONObject();
		
	    complaintsDao.updateComplaints(complaints);
	    resultObj.put(RE_CODE_STR, RE_CODE_0);
	    return resultObj.toString();
	}
	
	
    
}